// pages/login/pass/pass.js
const $api = require('../../../utils/api.js').API;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mobile: '',
    password: '',
    uimg1: false,
  },
  onLoad: function (options) {
    console.log(options)
    let _this = this
    _this.setData({
      zbType: options.zbType ? options.zbType : ''
    })
    $api.getloginconfig({
    }).then((res) => {
      _this.setData({
        Typephone: res.data.data.phone,
        Typepwd: res.data.data.pwd,
      })
    })
    //获取openid
    if (!(wx.getStorageSync('u'))) {
      wx.login({
        success(res) {
          $api.getOpenid({
            js_code: res.code
          }).then((res) => {
            wx.setStorageSync('u', res.data.data.openid)
          })
        }
      })
    }
  },
  //获取手机号
  onMobile(e) {
    this.setData({
      mobile: e.detail.value
    })
  },
  //获取密码
  onPassword(e) {
    this.setData({
      password: e.detail.value
    })
  },
  //验证码登录
  onCodeLogin(e) {
    let type = e.currentTarget.dataset.type
    let _this = this
    wx.redirectTo({
      url: '/pages/login/phone/phone?type=' + type + '&zbType=' + _this.data.zbType,
    })
  },
  //登录
  onLogin() {
    var _this = this;
    var mobile = _this.data.mobile;
    var password = _this.data.password;
    if (mobile == '') {
      wx.showToast({
        title: '请输入账号',
        icon: 'none',
        duration: 2000
      })
    } else if (password == '') {
      wx.showToast({
        title: '请输入密码',
        icon: 'none',
        duration: 2000
      })
    } else if (!_this.data.uimg1) {
      wx.showToast({
        title: '请勾选协议',
        icon: 'none',
        duration: 2000
      })
    } else {
      _this.setData({
        loadModal: true
      })
      $api.loginByPassword({
        user_name: mobile,
        password: password,
        openid: wx.getStorageSync('u')
      }).then((res) => {
        _this.setData({
          loadModal: false
        })
        wx.setStorageSync('login_token', res.data.login_token);
        wx.setStorageSync('is_admin', res.data.user_info.is_admin);
        wx.setStorageSync('avatar', res.data.user_info.avatar);
        wx.setStorageSync('nick_name', res.data.user_info.nick_name);
        wx.setStorageSync('is_prod_user', res.data.user_info.is_prod_user);
        wx.setStorageSync('user_name', res.data.user_info.user_name);
        wx.setStorageSync('user_id', res.data.user_info.user_id);
        wx.setStorageSync('sceneShow', true)
        if(_this.data.zbType == 1){
          wx.redirectTo({
            url: '/pages/live/live',
          })
        }else if (res.data.user_info.is_admin == 1 || res.data.user_info.is_admin == 2) {
          wx.redirectTo({
            url: '/pages/index/index',
          })
        } else {
          wx.redirectTo({
            url: '/pages/home/home',
          })
        }


      }).catch((err) => {
        _this.setData({
          loadModal: false
        })
      })
    }
  },
  //进入协议
  onAgreement(e) {
    wx.navigateTo({
      url: '/pages/agreement/agreement?id=' + e.currentTarget.dataset.id,
    })
  },
  onAgreementIcon(e) {
    this.setData({ uimg1: this.data.uimg1 ? false : true })
  }
})